package atom;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : AtomDemo2
 * @Author : 54061
 * @Date: 2022/2/16 21:02
 */
public class AtomDemo2 {
    public static void main(String[] args) {
        Target2 target = new Target2();
        for (int i = 0; i <100;i++){
            new Thread(target).start();
        }

    }
}
//使用原子性的类来解决非原子性操作的线程安全问题
class Target2 implements Runnable{
    //默认值是0
    private AtomicInteger count = new AtomicInteger();
    @Override
    public void run() {
        for (int i =0; i <1000;i++){
            //count++//非原子性的操作

            count.incrementAndGet();
            System.out.println("已经送了"+count+"个冰淇淋");
        }
    }
}